package cn.tedu.zhyl.wx.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.tedu.zhyl.base.constant.Constants;
import cn.tedu.zhyl.base.properties.JwtProperties;
import cn.tedu.zhyl.base.utils.JwtUtil;
import cn.tedu.zhyl.system.pojo.vo.LoginVO;
import cn.tedu.zhyl.wx.mapper.MemberMapper;
import cn.tedu.zhyl.wx.pojo.dto.UserLoginRequestDTO;
import cn.tedu.zhyl.wx.pojo.entity.WxMember;
import cn.tedu.zhyl.wx.pojo.vo.UserLoginVo;
import cn.tedu.zhyl.wx.service.MemberService;
import cn.tedu.zhyl.wx.service.WeChatService;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private WeChatService weChatService;
    @Autowired
    private MemberMapper memberMapper;

    static ArrayList DEFAULT_NICKNAME_PREFIX = Lists.newArrayList(
            "生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
    );

    @Override
    public UserLoginVo login(UserLoginRequestDTO dto) {
        String openid = weChatService.getOpenid(dto.getCode());
        WxMember member = memberMapper.getByOpenId(openid);

        if(ObjectUtil.isEmpty(member)){
            member = WxMember.builder().openId(openid).build();
        }

//        String phone = weChatService.getPhone(dto.getPhoneCode());
        String phone = dto.getPhoneCode();
        saveOrUpdate(member,phone);
        // 生成token
        Map<String, Object> claims = new HashMap<>();
        claims.put(Constants.JWT_USERID,member.getId());
        claims.put(Constants.JWT_USERNAME,member.getName());
        String token = JwtUtil.createJWT(jwtProperties.getSecret(),jwtProperties.getTtl(),claims);

        // 存入redis
        redisTemplate.opsForValue().set(Constants.WX_USER_LOGIN_API+token, JSON.toJSONString(member),60, TimeUnit.MINUTES);

        UserLoginVo userLoginVo = new UserLoginVo();
        userLoginVo.setToken(token);
        userLoginVo.setNickName(member.getName());
        return userLoginVo;
    }

    private void saveOrUpdate(WxMember member, String phone) {
        if(ObjectUtil.notEqual(phone,member.getPhone())){
                member.setPhone(phone);
        }

        if(ObjectUtil.isNotEmpty(member.getId())){
            memberMapper.update(member);
            return;
        }


        String nickName = DEFAULT_NICKNAME_PREFIX.get((int)(Math.random()*DEFAULT_NICKNAME_PREFIX.size()))+phone.substring(7);;
        member.setName(nickName);
        memberMapper.insert(member);

    }
}
